// HTTP client components
import { httpClass } from './libvbar-m-dxhttp.so'

const httpObj = new httpClass();

const http = {

    HTTP_METHOD : {
        GET: "GET",
        POST: "POST",
        PUT: "PUT",
        DELETE: "DELETE",
        HEAD: "HEAD",
        OPTIONS: "OPTIONS",
        PATCH: "PATCH"
    },
    HTTP_FORMAT : {
        JSON: "JSON",
        FORM: "FORM",
        URLENCODE: "URLENCODE"
    },
    HTTP_FORM_TYPE : {
        STRING: "STRING",
        FILE: "FILE"
    },
    /**
     * GET request
     * @param {string} url 
     * @param {array} headers Optional fields will be filled in by default in the request headers.
     * @param {number} timeout Optional field, timeout period
     * @returns 
     */
    get: function (url, headers, timeout) {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        return httpObj.request({ method: 0, url: url, headers: headers, timeout: timeout})
    },
    /**
     * POST request, data is in JSON/Form array format.
     * @param {string} url 
     * @param {array} data
     * @param {array} headers Optional fields will be filled in by default in the request headers.
     * @param {number} timeout Optional field, timeout period
     * @returns 
     */
    post: function (url, data, headers, timeout, format = "JSON") {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        if (!data) {
            throw new Error("data should not be null or empty")
        }
        if (typeof data != 'string' && format != "FORM") {
            data = JSON.stringify(data)
        }
        if(format == "JSON"){
            return httpObj.request({ method: 1, url: url, data: data, headers: headers, timeout: timeout})
        }else{
            return httpObj.request({ method: 1, url: url, formData: data, headers: headers, timeout: timeout})
        }
    },
    /**
     * A PUT request, where the data is a JSON/form array format.
     * @param {string} url 
     * @param {array} data
     * @param {array} headers Optional fields will be filled in by default in the request headers.
     * @param {number} timeout Optional field, timeout period
     * @returns 
     */
    put: function (url, data, headers, timeout, format = "JSON") {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        if (!data) {
            throw new Error("data should not be null or empty")
        }
        if (typeof data != 'string' && format != "FORM") {
            data = JSON.stringify(data)
        }
        if(format == "JSON"){
            return httpObj.request({ method: 2, url: url, data: data, headers: headers, timeout: timeout})
        }else{
            return httpObj.request({ method: 2, url: url, formData: data, headers: headers, timeout: timeout})
        }
    },
    /**
     * delete request
     * @param {string} url 
     * @param {array} headers Optional fields will be filled in by default in the request headers.
     * @param {number} timeout Optional field, timeout period
     * @returns 
     */
    delete: function (url, headers, timeout) {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        return httpObj.request({ method: 3, url: url, headers: headers, timeout: timeout})
    },
    /**
     * Downloading a file is essentially a GET request.
     * @param {string} url 
     * @param {string} path Target path (absolute path)
     * @param {array} headers Optional fields will be filled in by default in the request headers.
     * @param {number} timeout Optional field, timeout period
     * @returns The downloaded file may return null, but the download is successful.
     */
    download: function (url, path, headers, timeout) {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        if (!path) {
            throw new Error("path should not be null or empty")
        }
        return httpObj.request({ method: 0, url: url, headers: headers, download: path, timeout: timeout })
    },

    /**
     * Uploading a file is essentially a POST request.
     * @param {string} url 
     * @param {string} path Source path (absolute path)
     * @returns 
     */
    upload: function (url, path) {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        if (!path) {
            throw new Error("path should not be null or empty")
        }
        return httpObj.request({
            method: 1,
            url: url,
            headers: ["application/x-www-form-urlencoded; charset=UTF-8"],
            upload: path
        })
    },
    /**
     * native method
     * Required parameters: method (0: GET request, 1: POST request), url
     * Optional parameters: headers (string array, overriding the default header), download (file download address), data (request message, required for POST requests), timeout (timeout period/ms, default: 5000), dns (default: "8.8.8.8"), upload
     * Default header: Accept-Charset:utf-8, Content-Type: application/json; charset=utf-8, Connection:close
     * @param {object} param json
     *  like：let param={
                method:0,
                url:"http://192.168.10.122:8000/DW200_1_0.zip",
                download:"/testNet/aaa"
            }
     * @returns 
     */
    request: function (param) {
        return httpObj.request(param)
    }
}
export default http;
